//
//  HLIMKitAudioContentConfig.m
//  YueYue
//
//  Created by hulu001 on 2024/1/12.
//

#import "HLIMKitAudioContentConfig.h"
#import "HLChatKit.h"
#import <M80AttributedLabel/M80AttributedLabel.h>

@interface HLIMKitAudioContentConfig ()

@property (strong, nonatomic) M80AttributedLabel *contentLabel;

@end

@implementation HLIMKitAudioContentConfig


/// 计算气泡尺寸
/// @param cellWidth cell的宽
/// @param message 消息
- (CGSize)contentSize:(CGFloat)cellWidth message:(NIMMessage *)message
{
    HLIMKitMessageSetting *setting = [HLChatKit.shareSDK.config setting:message];
    CGFloat bubbleWidth    = 200;
    CGFloat bubbleLeftToContent  = setting.contentInsets.left;
    CGFloat contentRightToBubble = setting.contentInsets.right;
    CGFloat maxWidth = (bubbleWidth - contentRightToBubble - bubbleLeftToContent);
    CGFloat minWidth = 80;
    
    CGFloat minSeconds = 10;
    CGFloat maxSeconds = 60;
    
    NIMAudioObject *object = (NIMAudioObject *)message.messageObject;
    
    CGFloat w = minWidth;
    CGFloat h = 42;
    
    if (object.duration/1000 <= minSeconds) {
        return CGSizeMake(w + 50 , h);
    }
    
    NSTimeInterval seconds = object.duration/1000;
    
    w = w + seconds * 1.f/(maxSeconds-minSeconds) * (maxWidth-w);
    return CGSizeMake(w + 50, h);
}

/// content view 的名字（一般是类名）
/// @param message 消息
- (NSString *)cellContent:(NIMMessage *)message
{
    return @"HLIMKitMessageAudioContentView";
}

/// cell内容距离气泡的内间距
/// @param message 消息
- (UIEdgeInsets)contentViewInsets:(NIMMessage *)message;
{
    return [[HLChatKit.shareSDK config] setting:message].contentInsets;
}

#pragma mark - contentLabel

- (M80AttributedLabel *)contentLabel
{
    if (!_contentLabel) {
        _contentLabel = [[M80AttributedLabel alloc] initWithFrame:CGRectZero];
        _contentLabel.backgroundColor = UIColor.clearColor;
        _contentLabel.font = [UIFont systemFontOfSize:16.f];
    }
    return _contentLabel;
}


@end
